<?php
if(!isset($_POST['verifySign'])){
    JSON_R(403, '无法访问！', '');
}


$timeStamp=dataCheck($_POST['timeStamp']);
$verifySign=dataCheck($_POST['verifySign']);
if(!$timeStamp || ((time() - 60) > $timeStamp)){
    JSON_R(9001, '访问超时，请刷新重试或者请核对您的时间是否为正确北京(+86)时间。', '');
}
if(($timeStamp -  60) > time()){
    JSON_R(9002, '客户端时间与服务器时间相差过大！请核对您的时间是否为正确北京(+86)时间。','');
}

if(md5('timeStamp:'.$timeStamp.';str:pPfeky6axm21o3oY5CdVuONFgf34fsd') != $verifySign){
    JSON_R(9003, '签名有误！', '');
}
$inviteCode=dataCheck($_POST['inviteCode']);
if($inviteCode != '520'){
    JSON_R(403, '邀请码有误！', '');
}
//敏感词过滤 不过滤密码
//引入屏蔽处理类
include_once __DIR__.'/keyword_shielding/filterWords.php';
$logic = new filterWords();

$domainName=dataCheck($_POST['domainName']);//不能包含特殊符号
$domainName = $logic->filter($domainName,2,2);
$pattern = '/^[a-z0-9\x{4e00}-\x{9fa5}]+$/u';
if(!preg_match($pattern, $domainName)) {
    JSON_R(403, '域名名称 - 此值为全站唯一值，并且只能填写数字、小写字母、中文；不能出现其他特殊字符或表情！', '');
}
/*
PHP自带的判断是否是中文，
eregi('[^\x00-\x7F]',$str ) //中文
eregi('[0-9]',$str) //数字
eregi('[a-zA-Z]',$str)//英文
*/
$object=dataCheck($_POST['object']);
if(!$inviteCode || !$domainName || !$object){
    JSON_R(9004, '必填项不能为空！', '');
}
$siteTitle=dataCheck($_POST['siteTitle']);
$whisper=dataCheck($_POST['whisper']);
$pass=$_POST['pass'];

$siteTitle = $logic->filter($siteTitle,2,2);
$object = $logic->filter($object,2,2);
$whisper = $logic->filter($whisper,2,2);

$va=[
    [
        'val'=>$domainName,
        'name'=>'域名名称',
        'num'=>20,
    ],
    [
        'val'=>$siteTitle,
        'name'=>'网页标题',
        'num'=>200,
    ],
    [
        'val'=>$object,
        'name'=>'对方姓名',
        'num'=>40,
    ],
    [
        'val'=>$whisper,
        'name'=>'想说的话',
        'num'=>800,
    ],
    [
        'val'=>$pass,
        'name'=>'私密暗号',
        'num'=>20,
    ],
];
foreach ($va as $v){
    validation($v['val'],$v['name'],$v['num']);
}

$domainName=idn_to_utf8($domainName);
$dataQuery = $this->db->select()->from('table.contents')
    ->where('title = ?', $domainName.$identification)->limit(1);
$dataQueryEnd = $this->db->fetchRow($dataQuery);
if($dataQueryEnd){
    JSON_R(9005, '域名名称已存在，请更换一个名称！', '');
}

$data=[];
$data['title']=$domainName.$identification;
$data['slug']=md5($data['title']);
$data['created']=time();
$data['modified']=time();
$data['text']=json_encode([
    "title"=>$siteTitle,
    "object"=>$object,
    "whisper"=>$whisper,
]);
$data['password']=$pass;

$this->db->query($this->db->insert('table.contents')
    ->rows($data));

$d='访问地址为：http://'.$domainName.'.'.$siteUrl;
JSON_R(0, '创建成功！'.$d, ['url'=>'http://'.$domainName.'.'.$siteUrl]);

